<template>
  <el-dialog
    class="-assignrole"
    title="分配角色"
    :visible="showRoleDialog"
    :close-on-click-modal="false"
    @close="closeRoleDialog"
    @open="openRoleDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList" v-loading="isLoading">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-button
        type="primary"
        size="small"
        @click="assignRoles"
      >确定</el-button>
      <el-button size="small" @click="closeRoleDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getBaseUserInfoApi } from '@/api/user'
import { assignRolesApi } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [],
      roleList: [],
      isLoading: false
    }
  },
  methods: {
    /**
     * 关闭弹框
     */
    closeRoleDialog() {
      this.$emit('update:showRoleDialog', false)
    },
    /**
     * 打开弹框
     */
    openRoleDialog() {
      // 等所有异步请求都回来后才才执行then里面的代码
      // Promise.all([promise1,promise2,promise3]).then(()=>{

      // })
      this.isLoading = true
      Promise.all([this.getRoleList(), this.getBaseUserInfo()]).then(() => {
        this.isLoading = false
      })
    },
    /**
     * 获取角色列表
     */
    async getRoleList() {
      const { data } = await getRoleListApi(1, 999)
      this.roleList = data.rows
    },
    /**
     * 获取用户信息
     */
    async getBaseUserInfo() {
      const { data } = await getBaseUserInfoApi(this.userId)
      // console.log(data)
      this.checkList = data.roleIds
    },
    assignRoles() {
      assignRolesApi({
        id: this.userId,
        roleIds: this.roleIds
      })
      this.closeRoleDialog()
    }
  }
}
</script>

<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
